package com.jbh.recordlibrary.m;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;

import com.jbh.recordlibrary.i.PlayerWavListener;

import java.io.IOException;

public class MediaPlayerWavManager {


    private MediaPlayer mPlayer ;
    private boolean isPlaying = false ;
    private int currentLength;
    private int totalLength ;
    private PlayerWavListener playerWavListener;
    private static MediaPlayerWavManager mediaPlayerWavManager;

    public static MediaPlayerWavManager getInstance(){
        if (null == mediaPlayerWavManager){
            synchronized (MediaPlayerWavManager.class){
                if (null==null){
                    mediaPlayerWavManager  = new MediaPlayerWavManager();
                }
            }
        }
        return mediaPlayerWavManager ;
    }



    public void setPlayerWavListener(PlayerWavListener playerWavListener) {
        this.playerWavListener = playerWavListener;
    }


    /**
     * 开始播放  音频文件
     *
     * @param waveFile
     */
    public void start(String waveFile){
        try {
            mPlayer = new MediaPlayer();
            mPlayer.setDataSource(waveFile);
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            /*准备就绪*/
            mPlayer.prepare();
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (playerWavListener!=null){
                        playerWavListener.onStartPlay();
                    }
                    /*开始播放*/
                    mPlayer.start();
                    isPlaying = true ;
                    startTime();
                }
            });

            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    /*播放完成停止播放*/
                    stop();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    private Handler handler = new Handler();
    private int temp = 50;
    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            handler.postDelayed(this, temp);
            if (isPlaying){
                if (mPlayer!=null){
                    currentLength = mPlayer.getCurrentPosition();
                    totalLength = mPlayer.getDuration();
                }
                if (playerWavListener!=null){
                    playerWavListener.onProgress(currentLength,totalLength);
                }
            }
        }
    };


    /**
     * 是否正在播放
     *
     * @return
     */
    public boolean isPlaying(){
        return isPlaying ;
    }



    /**
     * 启动定时器
     */
    private void startTime(){
        handler.postDelayed(runnable, temp);
    }


    /**
     * 停止定时器
     */
    private void stopTime(){
        handler.removeCallbacks(runnable);
    }



    /**
     * 暂停播放
     */
    public void pausePlay(){
        if (mPlayer!=null&&mPlayer.isPlaying()){
            mPlayer.pause();
            isPlaying = false;
            if (playerWavListener!=null){
                currentLength = mPlayer.getCurrentPosition();
                playerWavListener.onPausePlay(currentLength);
            }
        }
    }


    /**
     * 重新播放
     */
    public void reStart(){
        if (mPlayer!=null&&!mPlayer.isPlaying()){
            isPlaying = true ;
            mPlayer.start();
            if (playerWavListener!=null){
                playerWavListener.onStartPlay();
            }
        }
    }



    /**
     * 指定位子播放
     *
     * @param msec
     */
    public void seekTo(int msec){
        if (mPlayer!=null){
            mPlayer.seekTo(msec);
        }
    }



    /**
     * 停止播放音频文件
     *
     * 释放
     */
    public void stop(){
        if (playerWavListener!=null){
            playerWavListener.onStopPlay();
        }

        mPlayer.stop();
        mPlayer.release();
        isPlaying = false ;
        mPlayer = null ;

        currentLength = 0 ;
        totalLength = 0 ;
        stopTime();
    }



}
